﻿using Common.DTO;
using PlayerAPI.Models.UserAccount;
using PlayerLogic;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;

namespace PlayerAPI.Controllers
{
    public class UserAccountController : ApiController
    {
        private UserAccountManager _userAccountManager;

        public UserAccountController()
        {
            _userAccountManager = new UserAccountManager();
        }

        [HttpGet]
        public HttpResponseMessage Get(Guid id)
        {
            var userAccount = _userAccountManager.Get(id);

            return Request.CreateResponse(HttpStatusCode.OK, userAccount);
        }

        [HttpPost]
        public HttpResponseMessage Add(UserAccountCreateRequestModel model) 
        {
            if (model == null ||
                String.IsNullOrEmpty(model.Name) ||
                String.IsNullOrEmpty(model.Email))
            {
                return Request.CreateResponse(HttpStatusCode.BadRequest, "Request model is invalid. No NULL values");
            }

            var userAccount = new UserAccountDTO
            {
                Name = model.Name,
                Email = model.Email
            };

            var guid = _userAccountManager.New(userAccount);

            return Request.CreateResponse(HttpStatusCode.OK, guid);
        }

        [HttpPost]
        public HttpResponseMessage Change(UserAccountChangeRequestModel model)
        {
            if (model == null ||
                String.IsNullOrEmpty(model.Name) ||
                String.IsNullOrEmpty(model.Email))
            {
                Request.CreateResponse(HttpStatusCode.BadRequest, "Request model is invalid. No NULL values");
            }

            var userAccount = new UserAccountDTO
            {
                Id = model.Id,
                Name = model.Name,
                Email = model.Email
            };

            var result = _userAccountManager.Change(userAccount);

            return Request.CreateResponse(HttpStatusCode.OK, result);
        }

        [HttpPost]
        public HttpResponseMessage Remove(Guid id)
        {
            var result = _userAccountManager.Remove(id);

            return Request.CreateResponse(HttpStatusCode.OK, result);
        }


    }
}
